package com.jh.fcsm.controller.area;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.excel.EasyExcel;
import com.jh.fcsm.beans.area.ImportError;
import com.jh.fcsm.beans.basic.vo.BasicAreaCodeVo;
import com.jh.fcsm.beans.sys.vo.SysUserVo;
import com.jh.fcsm.common.validation.group.DefaultGroup;
import com.jh.fcsm.common.validation.group.PageGroup;
import com.jh.fcsm.util.AreaExcelUtil;
import com.jh.fcsm.util.poi.EasyExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.github.pagehelper.PageInfo;

import com.jh.fcsm.beans.area.BasicArea;
import com.jh.fcsm.beans.area.vo.BasicAreaVo;
import com.jh.fcsm.common.RestApiResponse;
import com.jh.fcsm.common.annotation.RepeatSubAnnotation;
import com.jh.fcsm.common.annotation.SystemLogAnnotation;
import com.jh.fcsm.common.BaseController;
import com.jh.fcsm.service.area.BasicAreaService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;


/**
 * 行政区划管理Controller
 *
 * @author linq
 * @date 2024-08-15
 */
@RestController
@RequestMapping("/area/area")
@Api(value = "行政区划管理", description = "行政区划管理")
@CrossOrigin
@Validated
public class BasicAreaController extends BaseController {
    @Autowired
    private BasicAreaService basicAreaService;

    private static final String PER_PREFIX = "btn:area:area:";

    private static List<ImportError> errors;

    /**
     * 新增行政区划管理
     *
     * @param basicArea 行政区划管理数据 json
     * @return RestApiResponse<?>
     * @author linq
     */
    @RepeatSubAnnotation
    @PostMapping("/save")
    @ApiOperation(value = "新增行政区划管理", notes = "新增行政区划管理")
    @SystemLogAnnotation(type = "行政区划管理", value = "新增行政区划管理")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "save')")
    public RestApiResponse<?> saveBasicArea(@Validated(DefaultGroup.class) @RequestBody BasicArea basicArea) {
        String id = basicAreaService.saveOrUpdateBasicArea(basicArea);
        return RestApiResponse.ok(id);
    }

    /**
     * 修改行政区划管理
     *
     * @param basicArea 行政区划管理数据 json
     * @return RestApiResponse<?>
     * @author linq
     */
    @RepeatSubAnnotation
    @PostMapping("/update")
    @ApiOperation(value = "修改行政区划管理", notes = "修改行政区划管理")
    @SystemLogAnnotation(type = "行政区划管理", value = "修改行政区划管理")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "update')")
    public RestApiResponse<?> updateBasicArea(@Validated(DefaultGroup.class) @RequestBody BasicArea basicArea) {
        String id = basicAreaService.saveOrUpdateBasicArea(basicArea);
        return RestApiResponse.ok(id);
    }

    /**
     * 批量删除行政区划管理(判断 关联数据是否可以删除)
     *
     * @param ids
     * @return RestApiResponse<?>
     * @author linq
     */
    @RepeatSubAnnotation
    @PostMapping("/delete")
    @ApiOperation(value = "批量删除行政区划管理", notes = "批量删除行政区划管理")
    @SystemLogAnnotation(type = "行政区划管理", value = "批量删除行政区划管理")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "delete')")
    public RestApiResponse<?> deleteBasicArea(@RequestBody List<String> ids) {
        //判断 关联数据是否可以删除
        basicAreaService.deleteBasicArea(ids);
        return RestApiResponse.ok();
    }

    /**
     * 查询行政区划管理详情
     *
     * @param id
     * @return RestApiResponse<?>
     * @author linq
     */
    @GetMapping("/findById")
    @ApiOperation(value = "查询行政区划管理详情", notes = "查询行政区划管理详情")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "find')")
    public RestApiResponse<?> findById(@RequestParam("id") String id) {
        BasicArea basicArea = basicAreaService.findById(id);
        return RestApiResponse.ok(basicArea);
    }

    /**
     * 分页查询行政区划管理
     *
     * @param basicAreaVo 行政区划管理 查询条件
     * @return RestApiResponse<?>
     * @author linq
     */
    @PostMapping("/findPageByQuery")
    @ApiOperation(value = "分页查询行政区划管理", notes = "分页查询行政区划管理")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "query')")
    public RestApiResponse<?> findPageByQuery(@Validated(PageGroup.class) @RequestBody BasicAreaVo basicAreaVo) {
        PageInfo<BasicArea> basicArea = basicAreaService.findPageByQuery(basicAreaVo);
        return RestApiResponse.ok(basicArea);
    }

    /**
     * Excel导入行政区划数据
     *
     * @param file
     * @return
     */
    @PostMapping("/importExcel")
    @ApiOperation(value = "导入行政区划数据", notes = "通过Excel文件导入行政区划数据")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "import')")
    public RestApiResponse<?> importArea(@RequestParam("file") MultipartFile file, HttpServletResponse response) throws IOException {
        errors = basicAreaService.importAreaData(file, response);
        if (!errors.isEmpty()) {
            return RestApiResponse.error("请查看错误清单并修正后重新导入！");
        }else {
            return RestApiResponse.ok();
        }
    }

    @PostMapping("/generateErrorReport")
    public void generateErrorList(HttpServletResponse response) throws IOException {
		basicAreaService.generateErrorReport(errors, response);
    }


    /**
     * 导出行政区划数据
     *
     * @param basicAreaVo
     * @param response
     * @throws IOException
     */
    @PostMapping("/exportExcel")
    @ApiOperation(value = "导出行政区划数据", notes = "通过Excel文件导出行政区划数据")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "export')")
    public void exportExcel(@RequestBody BasicAreaVo basicAreaVo, HttpServletResponse response) throws IOException {
        PageInfo<BasicArea> pageInfo = basicAreaService.findPageByQuery(basicAreaVo);
        List<BasicArea> basicAreaList = pageInfo.getList();

        basicAreaService.exportAreaData(basicAreaList, response);
    }

}
